package com.moive.news.tqnewsapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.Adapter.BaseListAdapter;
import com.moive.news.tqnewsapplication.entity.MyProInfo;
import com.moive.news.tqnewsapplication.entity.NewsInfo;
import com.moive.news.tqnewsapplication.entity.ResourcesManager;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.ui.activity.DymicDetilsActivity;
import com.moive.news.tqnewsapplication.ui.activity.PersonDetilsActivity;
import com.moive.news.tqnewsapplication.utils.DateUtil;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PackageUtils;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import onekeyshare.OnekeyShare;

import static android.R.attr.button;

/**
 * Created by Administrator on 2017/12/14.
 * 评论
 */

public class MyProAdapter extends BaseListAdapter<MyProInfo.DataBean> {
    Context context;

    public MyProAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected View getItemView(int position, View convertView, ViewGroup parent, List<MyProInfo.DataBean> item) {
     ViewHelder viewHelder = null;
        if (viewHelder == null) {
          viewHelder = new ViewHelder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_mypro, null);
            viewHelder.ad_mypro_img = convertView.findViewById(R.id.ad_mypro_img);
            viewHelder.ad_mypro_nichen = convertView.findViewById(R.id.ad_mypro_nichen);
            viewHelder.ad_mypro_time = convertView.findViewById(R.id.ad_mypro_time);
            viewHelder.ad_mypro_desrc = convertView.findViewById(R.id.ad_mypro_desrc);
            viewHelder.ad_mypro_nine = convertView.findViewById(R.id.ad_mypro_nine);
            viewHelder.mypro_fenxiang_ll = convertView.findViewById(R.id.mypro_fenxiang_ll);
            viewHelder.mypro_pinglun_ll = convertView.findViewById(R.id.mypro_pinglun_ll);
            viewHelder.mypro_pinglun_text = convertView.findViewById(R.id.mypro_pinglun_text);
            viewHelder.mypro_dianzan_ll = convertView.findViewById(R.id.mypro_dianzan_ll);
            viewHelder.mypro_dianzan_text = convertView.findViewById(R.id.mypro_dianzan_text);
            viewHelder.mypro_dianzan_img = convertView.findViewById(R.id.mypro_dianzan_img);
            convertView.setTag(viewHelder);
        } else {
            viewHelder = (ViewHelder) convertView.getTag();
        }
        Glide.with(context)
                .load(item.get(position).getUserHeadImg())
                .centerCrop()
                .dontAnimate()
                .placeholder(R.drawable.icon)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHelder.ad_mypro_img);

        viewHelder.ad_mypro_nichen.setText(item.get(position).getUsername());
        String time = DateUtil.getDescriptionTimeFromTimestamp(item.get(position).getCreateDate());
        viewHelder.ad_mypro_time.setText(time);
        viewHelder.ad_mypro_desrc.setText(item.get(position).getContent());
        ArrayList<ImageInfo> imageInfo = new ArrayList<>();
        List<MyProInfo.DataBean.ImagesBean> list = item.get(position).getImages();
        for (MyProInfo.DataBean.ImagesBean imagesBean : list) {
            ImageInfo info = new ImageInfo();
            info.setThumbnailUrl(imagesBean.getThumbUrl());
            info.setBigImageUrl(imagesBean.getLargeUrl());
            imageInfo.add(info);
        }
        viewHelder.ad_mypro_nine.setAdapter(new NineGridViewClickAdapter(context, imageInfo));
        viewHelder.mypro_fenxiang_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOneKeyShare(item.get(position));
            }
        });
        viewHelder.mypro_pinglun_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DymicDetilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", item.get(position));
                LogUtil.i("lidaming",item.get(position).toString());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        viewHelder.mypro_pinglun_text.setText(item.get(position).getCommentCount() + "");
        viewHelder.mypro_dianzan_text.setText(item.get(position).getLikesCount() + "");
        if (item.get(position).getIsCurrentUserLikes()==1){
            viewHelder.mypro_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan02);
        }else {
            viewHelder.mypro_dianzan_img.setBackgroundResource(R.drawable.icon_dianzan01);
        }

        viewHelder.mypro_dianzan_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DymicDetilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", item.get(position));
                LogUtil.i("lidaming",item.get(position).toString());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
public    class ViewHelder {
        private CircleImageView ad_mypro_img;
        private TextView ad_mypro_nichen;
        private TextView ad_mypro_time;
        private TextView ad_mypro_desrc;
        private NineGridView ad_mypro_nine;
        private LinearLayout mypro_fenxiang_ll;
        private LinearLayout mypro_pinglun_ll;
        private TextView mypro_pinglun_text;
        private LinearLayout mypro_dianzan_ll;
        private TextView mypro_dianzan_text;
        private ImageView mypro_dianzan_img;
    }
    //分享
    private void onOneKeyShare(MyProInfo.DataBean item){
        OnekeyShare oks = new OnekeyShare();
        oks.setText(item.getContent());
        oks.setTitle("爱小道分享");
        oks.setTitleUrl("www.tqiant.com");
        if (item.getImages().size()!=0&&item.getImages()!=null){
            oks.setImageUrl(item.getImages().get(0).getThumbUrl());
        }else {
            oks.setImageUrl("www.tqiant.com");
        }
        oks.setSilent(true);
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + " completed at " + msg;
                ToastUtils.ShortToast("分享成功！");
            }
            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + "caught error at " + msg;
                ToastUtils.ShortToast("分享失败！");
            }
            @Override
            public void onCancel(Platform platform, int i) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + " canceled at " + msg;
//                ToastUtils.ShortToast(text);
            }
        });
        Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        String label = context.getString(R.string.app_name);
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                String text = "Customer Logo -- ShareSDK " + ShareSDK.getSDKVersionName();
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            }
        };
        oks.setCustomerLogo(logo, label, listener);
        oks.show(context);
    }

}
